from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta



class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.title
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    description = models.TextField()
    borrowed_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='borrowed_books')

    def __str__(self):
        return self.title
class Book(models.Model):
    borrowed_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='borrowed_books')
    borrowed_date = models.DateField(null=True, blank=True)
    due_date = models.DateField(null=True, blank=True)

    def renew(self, days=14):
        if self.due_date:
            self.due_date += timedelta(days=days)
        else:
            self.due_date = timezone.now().date() + timedelta(days=days)
        self.save()
class Author(models.Model):
    name = models.CharField(max_length=100)
    bio = models.TextField(blank=True)

    def __str__(self):
        return self.name


from django.contrib.auth.models import User



class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, default=1)
    description = models.TextField(blank=True)  # 新增介绍字段
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, default=1)
    description = models.TextField(blank=True)
    borrowed_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='borrowed_books')
    summary = models.TextField('摘要', blank=True)

    def __str__(self):
        return self.title


        class Meta:
            model = Book
            fields = ['title', 'author', 'description', 'summary']


class Review(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    book = models.ForeignKey('Book', on_delete=models.CASCADE, related_name='reviews')
    content = models.TextField('书评内容')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.user.username} 对《{self.book.title}》的书评"



